/*! ******************************************************************************
 *
 * Created on 2022年3月16日
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/
package org.eclipse.swt.browser;

import cn.hutool.core.util.ArrayUtil;
import com.google.common.collect.Lists;
import org.eclipse.swt.widgets.Composite;
import org.yzbdl.lanius.authorize.verification.jar.JarValidate;
import org.yzbdl.lanius.core.net.CookieChangeListener;
import org.yzbdl.lanius.core.net.CookieManager;

import java.net.HttpCookie;
import java.util.List;

/**
 * 浏览器类, 统一设置好cookie等信息
 *
 * @author hs@yzbdl.ac.cn
 * @version 1.0
 * @date 2022-08-02 11:19
 *
 */
public class LaniusBrowser extends Browser {

    // header
    public static final String HEADER_MACHINE_CODE = "machine-code";
    public static final String HEADER_AGENT = "User-Agent";

    // value
    public static final String DEF_AGENT = "LaniusDesign";
    public static final String DEF_MACHINE_CODE = JarValidate.MACHINE_CODE;

    // private static UserCookieService userCookieService = UserCookieService.getInstance();

    static {
        // 监听cookie的修改
        CookieManager.addChangeListener(new CookieChangeListener() {
            @Override
            public void afterRemove(String uri, HttpCookie cookie) {
                Browser.clearSessions();
                // setCookies();
            }

            @Override
            public void afterRemoveAll() {
                Browser.clearSessions();
            }

            @Override
            public void afterAdd(String uri, HttpCookie cookie) {
                Browser.setCookie(cookie.toString(), uri);
            }
        });
    }

    /**
     * @param parent
     * @param style
     */
    public LaniusBrowser(Composite parent, int style) {
        super(parent, style);

        // 设置初始cookie
        // setCookies();
    }

    @Override
    public boolean setUrl(String url, String postData, String[] headers) {

        // 添加默认header
        List<String> headerList = null;
        if (headers != null) {
            headerList = Lists.newArrayList(headers);
        } else {
            headerList = Lists.newArrayList();
        }

        headerList.add(LaniusBrowser.HEADER_MACHINE_CODE + ": " + LaniusBrowser.DEF_MACHINE_CODE);
        headerList.add(LaniusBrowser.HEADER_AGENT + ": " + LaniusBrowser.DEF_AGENT);
        return super.setUrl(url, postData, ArrayUtil.toArray(headerList, String.class));
    }

}
